1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class MyView3 extends View { Paint mPaint = new Paint(); Paint whitePaint = new Paint(); private Bitmap bitmap; private Matrix mat = new Matrix(); private Xfermode mXfermode;
public MyView3(Context context) { this(context, null); }
public MyView3(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }
public MyView3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); whitePaint.setColor(Color.RED); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dossier_icon); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mat.setScale((float)getWidth() / bitmap.getWidth(), (float)getHeight() / bitmap.getHeight()); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int count = canvas.saveLayer(0, 0, getWidth(), getHeight(), mPaint); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, whitePaint); mPaint.setXfermode(mXfermode); canvas.drawBitmap(bitmap, mat, mPaint); mPaint.setXfermode(null); canvas.restoreToCount(count); } }
|